package work5;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @TODO
 * @Author xu
 * 2025/2/26
 */

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class CalendarDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入年份: ");
        int year = scanner.nextInt();

        System.out.print("请输入月份 (1-12): ");
        int month = scanner.nextInt();

        // 检查输入的月份是否有效
        if (month < 1 || month > 12) {
            System.out.println("无效的月份，程序结束。");
            return;
        }

        printCalendar(year, month);
        scanner.close();
    }

    public static void printCalendar(int year, int month) {
        // 创建一个日历对象
        Calendar calendar = new GregorianCalendar(year, month - 1, 1); // 月份从0开始

        // 获取该月的第一天是星期几
        int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // 获取该月的总天数
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        System.out.println(year + "年" + month + "月的日历:");
        System.out.println("日 一 二 三 四 五 六");

        // 打印空格
        for (int i = 1; i < firstDayOfWeek; i++) {
            System.out.print("   "); // 每个日期占3个字符
        }

        // 打印每一天
        for (int day = 1; day <= daysInMonth; day++) {
            System.out.printf("%2d ", day);
            if ((day + firstDayOfWeek - 1) % 7 == 0) {
                System.out.println(); // 换行
            }
        }
        System.out.println(); // 最后换行
    }
}
